<template>
  <div class="box">
    <div
      :fullscreen="true"
      v-loading.lock="loading"
      class="loading-content"
      v-if="loading"
    ></div>
    <div class="payment">
      <div class="qr-box">
        <div class="qr-img">
          <div ref="cbcCode"></div>
        </div>
      </div>
      <div class="info" v-if="!loading">
        <el-alert
          class="tips"
          :title="$t('walletTips')"
          type="warning"
          show-icon
          :closable="false"
        >
        </el-alert>
        <div class="info-item">
          <span class="rowLabel">{{ $t("chain_type") }}:</span>
          <span class="rowValue">{{ chain_type }}</span>
        </div>
        <div class="info-item">
          <span class="rowLabel">{{ $t("rechargeAddress") }}:</span>
          <span class="rowValue">{{ address }} </span>
        </div>
        <div class="copyBtn">
          <span class="textBtn" @click="copy">{{ $t("CopyAddress") }}</span>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import QRCode from "qrcodejs2";
import { copyText } from "@/utils/common";
import {
  httpWalletBalance,
  httpInfo,
  httpWalletAddress,
  httpWalletBind,
} from "@/http/api";
export default {
  name: "walletRecharge",
  data() {
    return {
      payWayTitle: "",
      chain_type: "",
      amount: "",
      address: "",
      form: { bind_email: "", bind_password: "" },
      rules: {
        bind_email: [
          {
            required: true,
            message: this.$t("walletEmailTip"),
            trigger: "change",
          },
        ],
        bind_password: [
          { required: true, validator: this.checkPwd, trigger: "blur" },
        ],
      },
      loading: false,
    };
  },
  created() {
    this.getHttpInfo();
  },
  methods: {
    copy() {
      copyText(this.address);
      this.$message.success(this.$t("copySuccess"));
    },
    checkPwd(rule, value, callback) {
      if (!value) {
        callback(new Error(this.$t("walletPsdTip")));
      } else {
        const checkPwdFormat = new RegExp(/^[0-9a-zA-Z]{6,16}$/);
        if (!checkPwdFormat.test(value)) {
          callback(new Error(this.$t("walletPsdTip1")));
          return;
        }
        callback();
      }
    },
    refresh() {
      this.getBalance();
    },
    getBalance() {
      httpWalletBalance().then((res) => {
        this.available = res.data.available || 0;
        this.coin_name = res.data.coin_name || "USD";
      });
    },
    async getHttpInfo() {
      try {
        this.loading = true;
        let res = await httpInfo();
        if (res.code == 0) {
          this.is_wallet = res.data?.is_wallet || 0;
          this.form.bind_email = res.data?.account_name || "";
          this.form.bind_email = res.data?.account_name || "";
          if (this.is_wallet == 1) {
            await this.getBalance();
            await this.getAddress();
          }
        }
        this.loading = false;
      } catch {
        this.loading = false;
      }
    },
    async getAddress() {
      let res = await httpWalletAddress();
      const { chain_name, address } = res.data;
      this.chain_name = chain_name;
      this.address = address;
      this.$nextTick(() => {
        new QRCode(this.$refs.cbcCode, {
          text: address,
          width: 200,
          height: 200,
          colorDark: "#000000",
          colorLight: "#ffffff",
          correctLevel: QRCode.CorrectLevel.H,
        });
      });
    },
    openWallet(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const res = httpWalletBind(this.form);
          this.is_wallet = res.data?.is_wallet || 0;
          if (this.is_wallet == 1) {
            this.getBalance();
            this.getRecord();
            this.getAddress();
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
.box {
  position: relative;
  width: 100%;
}
.loading-content {
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.payment {
  width: 100%;
  height: 260px;
  padding: 20px;
  display: flex;
  justify-content: start;
  align-items: center;
  background-color: #fff;
  .qr-box {
    width: 200px;
    .qr-img {
      width: 100%;
      margin: 0 auto;
      display: flex;
      justify-content: center;
    }
  }
  .info {
    flex: 1;
    height: 100%;
    background: #f4f6f9;
    margin-left: 20px;
    padding: 20px;
    display: flex;
    flex-direction: column;
    justify-content: start;
    align-items: start;
    position: relative;
    .tips {
      width: 100%;
      position: absolute;
      top: 0;
      left: 0;
    }
    .info-item {
      width: 100%;
      margin-top: 40px;
      flex: 1;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .rowLabel {
        display: inline-block;
        min-width: 130px;
        font-size: 14px;
      }
      .rowValue {
        margin-left: 10px;
        display: flex;
        justify-content: start;
        align-items: center;
        font-size: 14px;
      }
    }
    .copyBtn {
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 14px;
      width: 100%;
      margin-top: 25px;
      .textBtn {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 100px;
        height: 35px;
        line-height: 20px;
        background-color: #513216;
        border-radius: 5px;
        font-size: 12px;
        color: #fff;
        margin-left: 5px;
        text-align: center;
        cursor: pointer;
      }
    }
  }
}
.address-box {
  padding: 25px !important;
}
.pay-way-box {
  margin: 30px;
  .pay-way {
    // background: #ced4f8;
    background-color: #f9f3dd;
    // display: flex;
    justify-content: space-between;
    padding: 30px;
    border-radius: 15px;
    margin-bottom: 20px;
    .top {
      margin-bottom: 20px;
    }
    .bottom {
      margin-top: 20px;
    }
    .pay-item {
      display: flex;
      justify-content: space-between;
      height: 100%;
      .address {
        font-size: 24px;
      }
    }
  }
}
.btn {
  margin: 0 auto;
  margin-top: 50px;
  width: 682px;
  height: 76px;
  background: #f9f3dd;
  font-size: 28px;
  font-weight: 400;
  letter-spacing: 0px;
  // color: rgba(255, 255, 255, 1);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 20px;
  // transition: all 0.05s;
  user-select: none;
}
.el-alert--warning {
  color: #f35757;
}
</style>
